/**
 * 
 */
requirejs.config({
    baseUrl:'http://static.auoffice.com/static/js' ,
    waitSeconds: 0,
    paths: {
    	jquery:'libs/jquery.min',
    	bootstrap: 'libs/bootstrap/bootstrap',
    	validate:'libs/validate/jquery.validate.min',
    	iCheck: 'libs/iCheck/icheck.min',
    	jsTree:'libs/jsTree/jstree.min',
        //plugins
        base: 'common/base',
    },
    shim: {
    	'bootstrap': {
    		deps: ['jquery'],
    		exports: 'bootstrap'
    	},
    	'validate': {
    		deps: ['jquery'],
    		exports: 'validate'
    	},    	
    	'iCheck': {
    		deps: ['jquery'],
    		exports: 'iCheck'
    	},
    }
});

require([
    'jquery',
 	'common/base',
 	'jsTree',
 	'bootstrap',
 	'validate',
 	'iCheck'
],
function($,base) {
	var user_id = $("#user_id").val();
	$(document).ready(function(){
    	base.services.post('account', 'userFuncResourceList', {"user_id":user_id}, function(code, msg, data) {
    		if (code === base.services.CODE_SUCC) {
				$("#jstree1").jstree({
					"core":{"check_callback":true,"data":data.funcresources},
					"plugins":["checkbox"],
					"checkbox": {
						"keep_selected_style": false,//是否默认选中
					}
				});
    		} else {
    			base.layui.alert("错误信息",msg);
    		}
    	});
	});
	//点击确定按钮
	$(".save").click(function(){
		var nodes = $('#jstree1').jstree().get_checked(true);
		var node_ids = [];
		for (var node in nodes){
			if (nodes[node].parent != "#"){
				node_ids.push(nodes[node].id);
			}
		}
		if (node_ids.length <= 0){
			base.layui.alert("提示信息","请选择用户资源");
			return;
		}
		base.services.post('account', 'saveUserFuncResource', {"user_id":user_id,"node_ids":$.unique(node_ids).join(",")}, function(code, msg, data) {
			if (code === base.services.CODE_SUCC) {
				base.layui.alert("成功信息","保存成功");
			} else {
				base.layui.alert("错误信息",msg);
			}
		});
	});
});